﻿using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.NetworkInformation;
using System.Threading;
using System.Windows.Forms;

namespace SetDnsAction
{
    public partial class SetDnsAction : Form
    {
        public SetDnsAction()
        {
            InitializeComponent();
        }

        private void DnsBtn_Click(object sender, EventArgs e)
        {

            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface adapter in nics)
            {
                bool Pd1 = (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet); //判断是否是以太网连接  
                if (Pd1)
                {
                    string Networkname = adapter.Name;
                    IPAddress ip1 = IPAddress.Parse("172.168.10.180");
                    Ping ping = new Ping();
                    PingReply pr;
                    //PING失败了，不是内网用户
                    pr = ping.Send(ip1);
                    if (pr.Status != IPStatus.Success)
                    {
                        MessageBox.Show("不是内网用户");
                        break;
                    }

                    Process proc1 = new Process();
                    proc1.StartInfo.CreateNoWindow = true;
                    proc1.StartInfo.FileName = "cmd.exe";
                    proc1.StartInfo.Arguments = @"C:\Windows\System32\cmd.exe";
                    proc1.StartInfo.UseShellExecute = false;
                    proc1.StartInfo.RedirectStandardError = true;
                    proc1.StartInfo.RedirectStandardInput = true;
                    proc1.StartInfo.RedirectStandardOutput = true;
                    proc1.StartInfo.Verb = "RunAs";
                    proc1.Start();
                    proc1.StandardInput.WriteLine("netsh interface ip del dns name=" + Networkname + " all ");
                    //MessageBox.Show("netsh interface ip set dns " + Networkname + " static " + "172.168.1.5" + " primary");
                    proc1.StandardInput.WriteLine("exit");

                    proc1.Close();
                    Thread.Sleep(1000);

                    Process proc = new Process();
                    proc.StartInfo.CreateNoWindow = true;
                    proc.StartInfo.FileName = "cmd.exe";
                    proc.StartInfo.Arguments = @"C:\Windows\System32\cmd.exe";
                    proc.StartInfo.UseShellExecute = false;
                    proc.StartInfo.RedirectStandardError = true;
                    proc.StartInfo.RedirectStandardInput = true;
                    proc.StartInfo.RedirectStandardOutput = true;
                    proc.StartInfo.Verb = "RunAs";
                    proc.Start();
                    proc.StandardInput.WriteLine("netsh interface ip set dns " + Networkname + " static" + " 172.168.1.5" + " primary");
                    //MessageBox.Show("netsh interface ip set dns " + Networkname + " static " + "172.168.1.5" + " primary");
                    proc.StandardInput.WriteLine("exit");

                    proc.Close();

                    Thread.Sleep(2000);

                    Process proc2 = new Process();
                    proc2.StartInfo.CreateNoWindow = true;
                    proc2.StartInfo.FileName = "cmd.exe";
                    proc2.StartInfo.Arguments = @"C:\Windows\System32\cmd.exe";
                    proc2.StartInfo.UseShellExecute = false;
                    proc2.StartInfo.RedirectStandardError = true;
                    proc2.StartInfo.RedirectStandardInput = true;
                    proc2.StartInfo.RedirectStandardOutput = true;
                    proc2.StartInfo.Verb = "RunAs";
                    proc2.Start();
                    proc2.StandardInput.WriteLine("netsh interface ip add dns " + Networkname + " 172.168.1.6" + " index=2");
                    //MessageBox.Show("netsh interface ip add dns" + Networkname + " 172.168.1.6" + " index=2");
                    proc2.Close();
                    MessageBox.Show("更改DNS成功，可以正常使用");
                    break;

                }
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface adapter in nics)
            {
                bool Pd1 = (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet); //判断是否是以太网连接  
                if (Pd1)
                {
                    string Networkname = adapter.Name;
                    IPInterfaceProperties ip = adapter.GetIPProperties();     //IP配置信息  
                                                                              //当DNS符合条件，则修改DNS
                    if (ip.DnsAddresses.Count==1)
                    {
                        MessageBox.Show("首选DNS: " + ip.DnsAddresses[0].ToString());
                        break;
                    }
                    if (ip.DnsAddresses.Count == 2)
                    {
                        MessageBox.Show("首选DNS: " + ip.DnsAddresses[0].ToString());
                        MessageBox.Show("备选DNS: " + ip.DnsAddresses[1].ToString());
                        break; 
                    }
                    if (ip.DnsAddresses.Count<1)
                    {
                        MessageBox.Show("DNS为空，请先点击设置内网DNS 再查看");
                    }
                }
            }
        }
    }
}




#region 注释掉
//                    //IPInterfaceProperties ip = adapter.GetIPProperties();     //IP配置信息  
//                    //当DNS符合条件，则修改DNS
//                    // if (ip.DnsAddresses[0].ToString() == "172.168.10.18" || ip.DnsAddresses[0].ToString() == "172.168.10.21" || ip.DnsAddresses[0].ToString() == "172.168.10.10")
//                    // {
//                    Process proc1 = new Process();
//proc1.StartInfo.CreateNoWindow = true;
//                    proc1.StartInfo.FileName = "cmd.exe";
//                    proc1.StartInfo.Arguments = @"C:\Windows\System32\cmd.exe";
//                    proc1.StartInfo.UseShellExecute = false;
//                    proc1.StartInfo.RedirectStandardError = true;
//                    proc1.StartInfo.RedirectStandardInput = true;
//                    proc1.StartInfo.RedirectStandardOutput = true;
//                    proc1.StartInfo.Verb = "RunAs";
//                    proc1.Start();
//                    proc1.StandardInput.WriteLine("netsh interface ip del dns name=" + Networkname + " all ");
//                    //MessageBox.Show("netsh interface ip set dns " + Networkname + " static " + "172.168.1.5" + " primary");
//                    proc1.StandardInput.WriteLine("exit");

//                    proc1.Close();
//                    Thread.Sleep(1000);

//                    Process proc = new Process();
//proc.StartInfo.CreateNoWindow = true;
//                    proc.StartInfo.FileName = "cmd.exe";
//                    proc.StartInfo.Arguments = @"C:\Windows\System32\cmd.exe";
//                    proc.StartInfo.UseShellExecute = false;
//                    proc.StartInfo.RedirectStandardError = true;
//                    proc.StartInfo.RedirectStandardInput = true;
//                    proc.StartInfo.RedirectStandardOutput = true;
//                    proc.StartInfo.Verb = "RunAs";
//                    proc.Start();
//                    proc.StandardInput.WriteLine("netsh interface ip set dns " + Networkname + " static" + " 172.168.1.5" + " primary");
//                    //MessageBox.Show("netsh interface ip set dns " + Networkname + " static " + "172.168.1.5" + " primary");
//                    proc.StandardInput.WriteLine("exit");

//                    proc.Close();

//                    Thread.Sleep(2000);

//                    Process proc2 = new Process();
//proc2.StartInfo.CreateNoWindow = true;
//                    proc2.StartInfo.FileName = "cmd.exe";
//                    proc2.StartInfo.Arguments = @"C:\Windows\System32\cmd.exe";
//                    proc2.StartInfo.UseShellExecute = false;
//                    proc2.StartInfo.RedirectStandardError = true;
//                    proc2.StartInfo.RedirectStandardInput = true;
//                    proc2.StartInfo.RedirectStandardOutput = true;
//                    proc2.StartInfo.Verb = "RunAs";
//                    proc2.Start();
//                    proc2.StandardInput.WriteLine("netsh interface ip add dns " + Networkname + " 172.168.1.6" + " index=2");
//                    //MessageBox.Show("netsh interface ip add dns" + Networkname + " 172.168.1.6" + " index=2");
//                    proc2.Close();

//                    MessageBox.Show("更改DNS成功，可以正常使用");

//                    break;

//                    // } 
#endregion
